<?php


namespace app\tools;


class BillingPoint
{
    // 根据计费点id获取计费点数组
    public static function getBillingPointById($id){
        try {
            $json_string = file_get_contents(env('PLATFORM.BillingPoint'));
            if($json_string === false){
                throw new \Exception("file_get_contents error");
            }

            $data = json_decode($json_string, true);
            $json_error = json_last_error();
            if(empty($data) || $json_error != JSON_ERROR_NONE){
                throw new \Exception("json error:".$json_error);
            }

            if(!isset($data[$id])){
                throw new \Exception("not found ");
            }

            return $data[$id][0];
        }catch (\Exception $e){
            mylog('billing id '.$id. ' ' .$e->getMessage());
            return false;
        }

    }

    // 苹果计费点
    public static function CfgIOS($id){
        try {
            $json_string = file_get_contents(env('PLATFORM.CfgIOS'));
            if($json_string === false){
                throw new \Exception("file_get_contents error");
            }

            $data = json_decode($json_string, true);
            $json_error = json_last_error();
            if(empty($data) || $json_error != JSON_ERROR_NONE){
                throw new \Exception("json error:".$json_error);
            }

            if(!isset($data[$id])){
                throw new \Exception("not found ");
            }

            return $data[$id];
        }catch (\Exception $e){
            mylog('billing id '.$id. ' ' .$e->getMessage());
            return false;
        }

    }
    //根据计费点获取点券  
    public static function getBillingSMoney($id){

        $res = self::getBillingPointById($id);
        if(isset($res['SMoney'])){
            return $res;
        }
        $res['SMoney'] = $res['Price']*10;

        return $res;
    }
}